<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Car;


class SellCarController extends Controller
{
    /*获取到路由传过来的城市赋值在模板上面*/
    public function index(Request $request)
    {
        /*加载城市配置文件*/
        $cities = config('city');
        /*获取路由城市的第一个字母,转化成大写 作为配置文件的键*/
        $char = strtoupper(substr($request->city, 0, 1));
        /*获取到的城市拼音*/
        $selCity['pinyin'] = $request->city;
        /*在根据获取到的城市拼音取值*/
        $selCity['value'] = $cities[$char][$request->city];
        /*最后赋值到模板当中*/
        return view('sellcar',['selCity' => $selCity]);
    }

    /**
     * 用户点击"我要卖车"所提交上来的表单
     */
    public function addPost(Request $req)
    {

        //这里最好的方法 如果用户没有登录,跳转到登陆页面
            if ($req->user() == null) {
                return view('auth/login');
            } else {
                /*在这里需要进行判断用户的登陆情况*/
        		$car = new Car();
                $car->uid = $req->user()->uid;
                // dd($car->uid);
                $car->brand_name = $req->brand_name;
                $car->series_name = $req->series_name;
        		$car->reg_time=$req->reg_time;
                $pros=config('provinces');
        		$car->checkcar_pro=$pros[$req->checkcar_pro];
        		$car->checkcar_city=$req->checkcar_city;
        		$car->checkcar_addr=$req->checkcar_addr;
        		$car->mileage=$req->mileage;
        		$car->emission=$req->emission;
        		$car->license_addr=$req->license_addr;
                $car->seller_intro = $req->seller_intro;
        		if ($car->save()) {
                        $content = '您的信息已提交,请保持电话畅通,稍后我们将会和您联系!';
                        return view('success',['content'=>$content]);
                }
            }
    }
}
